<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-4-8
 * Time: 上午9:16
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Withdrawapply extends Modules_Controller
{

    function __construct()
    {
        parent::__construct();

        $this->rules = array(
            "rule" => array(
                array(
                    "field" => "title",
                    "label" => lang('title'),
                    "rules" => "trim|required|min_length[1]"
                )
                ,array(
                    "field" => "timeline",
                    "label" => lang('time'),
                    "rules" => "trim|strtotime"
                )
                ,array(
                    "field" => "content",
                    "label" => lang('conent'),
                    "rules" => "trim"
                    // link_create tag 生成
                )
                ,array(
                    "field" => "photo",
                    "label" => lang('photo'),
                    "rules" => "trim"
                )
            ),
            "batch" => array(
                array(
                    "field" => "WIDemail",
                    "label" => '付款账户邮箱',
                    "rules" => "trim|required|min_length[1]"
                )
                ,array(
                    "field" => "WIDaccount_name",
                    "label" => '付款账户名',
                    "rules" => "trim|strtotime"
                )
                ,array(
                    "field" => "WIDbatch_fee",
                    "label" => '付款总金额',
                    "rules" => "trim"
                    // link_create tag 生成
                )
                ,array(
                    "field" => "WIDbatch_num",
                    "label" => '付款笔数',
                    "rules" => "trim"
                )
            )
        );

    }

    public function _edit_data()
    {
        $form=$this->input->post();
        $form['timeline'] = time();
        return $form;
    }
    public function index($cid=false,$page=1)
    {
        // 栏目路径
        $vdata['cpath']= $this->mcol->get_path_more($this->cid);
        $vdata['cchildren'] = $this->mcol->get_cols($this->cid);
        $title = $this->mcol->get_one($this->cid,"title");
        $vdata['title'] = $title['title'];

        $limit = $this->page_limit;
        $this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');

        $order = $this->_index_orders();
        if ($this->input->get('order',TRUE)) {
            // TODO: order
            // $orders = explode("-",$this->input->get('order',TRUE));
            $order = $this->input->get('order',TRUE);
        }
        $where_in = array("withdraw_apply.cid"=>$this->cid,"withdraw_apply.ccid"=>$this->ccid);
        // 条件必须
        $where = array_merge($this->_index_where(),$where_in);
        $count = $this->model->get_count_all($where,false,array('account','account.id=withdraw_apply.uid','left'));
        $vdata['pages'] = $this->_pages_join(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$count,4);
        //$vdata['pages'] = $this->_pages(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$where,4);
        $fields = 'withdraw_apply.*,account.uname,account.phone';
        $vdata['list'] = $this->model->get_list($limit,$limit*($page-1),$order,$where,$fields,false,array('account','account.id=withdraw_apply.uid','left'));
        //$vdata['list'] = $this->model->get_list($limit,$limit*($page-1),$order,$where);
        $total = $this->model->get_status_nums();
        $vdata['total'] = $total['total'];
        $this->_display($vdata);
    }
    public function one() {
        $data = $this->input->get();

        $update['is_checked'] = $data['is_checked'];
        $update['updated'] = time();
        if($data['is_checked'] == 1) {
            $update['finished'] = time();
        }
        $res = $this->model->set($data['id'],$update);

        if($res) {
            if($data['is_checked']==3){
                $info = $this->model->get_one(array('id'=>$data['id'],'is_checked'=>3),'uid,money,partner_trade_no');
                //var_dump($info);die();
                if($info){
                    $this->load->model('account_model','macc');
                    $this->load->model('balance_detail_model','mbalance');
                    $this->macc->set_money($info['uid'],(int)$info['money']);
                   // die();
                    $balance = $this->mbalance->get_one(array('pay_code'=>$info['partner_trade_no']));
                    $this->mbalance->set($balance['id'],array('status'=>4,'updated_at'=>time()));
                }
            }
            if($data['is_checked'] == 1) {
                //$this->is_checked_after($data['id']);
            }
            redirect(base64_decode($data['ref']));
        }
    }
    public function copypro()
    {
        $ids = $this->input->post('ids');

        $rs=$this->model->get_one($ids);

        unset($rs['id']);
        unset($rs['sort_id']);
        unset($rs['timeline']);

        $id = $this->model->create($rs);
        if ($id) {
            $vdata['msg'] = '复制成功，请刷新查看';
            $vdata['status'] = 1;
        }else{
            $vdata['msg'] = '复制失败，请刷新后重试';
            $vdata['status'] = 0;
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
    }
    public function alibatch() {
        $data = $this->input->get();
        if(isset($data['id']) && $data['id']) {
            $batch = $this->model->get_one($data['id']);
            if(!$batch || $batch['is_checked']) {
                redirect(base64_decode($data['ref']));
            }
            $vdata['batch'] = $batch;
        }
        if(is_post()) {

            $this->form_validation->set_rules($this->_get_rule('batch'));
            if ($this->form_validation->run()) {
                require_once(LIBS_PATH."alipay_batch/alipay.config.php");
                require_once(LIBS_PATH."alipay_batch/lib/alipay_submit.class.php");

                /**************************请求参数**************************/

                //服务器异步通知页面路径
                //$notify_url = "http://商户网关地址/batch_trans_notify-PHP-UTF-8/notify_url.php";
                //需http://格式的完整路径，不允许加?id=123这类自定义参数

                //付款账号
                $email = $_POST['WIDemail'];
                //必填

                //付款账户名
                $account_name = $_POST['WIDaccount_name'];
                //必填，个人支付宝账号是真实姓名公司支付宝账号是公司名称

                //付款当天日期
                $pay_date = date('Ymd',time());
                //$pay_date = $_POST['WIDpay_date'];

                //必填，格式：年[4位]月[2位]日[2位]，如：20100801

                //批次号
                $batch_no = date('Ymd',time()).ordernum_time('',true);
                //$batch_no = $_POST['WIDbatch_no'];

                //必填，格式：当天日期[8位]+序列号[3至16位]，如：201008010000001

                //付款总金额
                $batch_fee = $_POST['WIDbatch_fee'];
                //必填，即参数detail_data的值中所有金额的总和

                //付款笔数
                $batch_num = $_POST['WIDbatch_num'];
                //必填，即参数detail_data的值中，“|”字符出现的数量加1，最大支持1000笔（即“|”字符出现的数量999个）

                //付款详细数据
                $detail_data = $_POST['WIDdetail_data'];
                //必填，格式：流水号1^收款方帐号1^真实姓名^付款金额1^备注说明1|流水号2^收款方帐号2^真实姓名^付款金额2^备注说明2....


                /************************************************************/

                //构造要请求的参数数组，无需改动
                $parameter = array(
                    "service" => $alipay_config['service'],
                    "partner" => trim($alipay_config['partner']),
                    "notify_url"	=> $alipay_config['notify_url'],
                    "email"	=> $email,
                    "account_name"	=> $account_name,
                    "pay_date"	=> $pay_date,
                    "batch_no"	=> $batch_no,
                    "batch_fee"	=> $batch_fee,
                    "batch_num"	=> $batch_num,
                    "detail_data"	=> $detail_data,
                    "_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
                );

                //建立请求
                $alipaySubmit = new AlipaySubmit($alipay_config);
               // var_dump($alipaySubmit);die();
                $html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认");
                echo $html_text;
            } else {
                var_dump(form_errors());die();
            }
        } else {
            $this->_display($vdata);
        }
    }

}
